package com.atguigu.servlet;

import javax.servlet.*;
import java.io.IOException;

/**
 * Date:2021/3/9
 * Author:ybc
 * Description:
 */
public class TestServlet implements Servlet {

    public TestServlet(){
        System.out.println("TestServlet-->构造方法");
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        //获取ServletContext对象
        ServletContext servletContext = servletConfig.getServletContext();
        //在ServletContext对象中共享数据
        servletContext.setAttribute("testServletContext", "呵呵");
        System.out.println("TestServlet-->初始化");
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("TestServlet-->服务");
        servletRequest.getRequestDispatcher("/success.html").forward(servletRequest, servletResponse);
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("TestServlet-->销毁");
    }
}
